package com.xquant.databasemanagement.controller.windows;

import com.xquant.databasemanagement.bean.table.ColumnBean;
import com.xquant.databasemanagement.bean.table.IndexesBean;
import com.xquant.databasemanagement.controller.FormController;
import com.xquant.databasemanagement.enums.IndexType;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.apache.commons.lang.StringUtils;

import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

public class IndexesWindowController implements Initializable, FormController {

    @FXML
    private TableView<Field> columnTable;
    @FXML
    private TextField tablespace;
    @FXML
    private VBox indexesWindow;

    @FXML
    private TextField indexesName;

    @FXML
    private ComboBox<IndexType> indexesType;

    @FXML
    private ComboBox<String> indexesColumn;

    private ObservableList<ColumnBean> columnNames;

    private IndexesBean indexesBean = null;

    @FXML
    public void eventSaveIndexesButtonClick(ActionEvent event) {
        if (isValid()) {
            Stage parentWin = (Stage) indexesWindow.getScene().getWindow();
            if (indexesBean == null) {
                setIndexesBean();
                TableView view = ((TableView) parentWin.getOwner().getScene().lookup("#indexesTable"));
                view.getItems().addAll(this.indexesBean);
            }else
                setIndexesBean();
            clear();
            parentWin.close();
        }
    }

    public void initColumnNames(ObservableList<ColumnBean> columnNames){
        indexesColumn.getItems().clear();
        this.columnNames = columnNames;
        for (ColumnBean p:this.columnNames) {
            indexesColumn.getItems().addAll(p.getColumnName());
        }

    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        indexesType.getItems().addAll(IndexType.values());
    }

    public void setIndexesBean(IndexesBean indexesBean){
        this.indexesBean = indexesBean;
        indexesName.setText(indexesBean.getIndexesName());
        String columns[] = indexesBean.getIndexesColumn().split(",");
        for (String column : columns) {
            columnTable.getItems().addAll(new Field(column));
        }
        indexesType.getSelectionModel().select(IndexType.valueOf(indexesBean.getIndexesType()));
        tablespace.setText(indexesBean.getTablespace());
    }


    private void setIndexesBean() {
        if (indexesBean == null)
            indexesBean = new IndexesBean();
        indexesBean.setIndexesName(indexesName.getText());
        indexesBean.setIndexesType(indexesType.getValue().toString());
        indexesBean.setTablespace(tablespace.getText());
        List<Field> fields = columnTable.getItems();
        indexesBean.setIndexesColumn(fields.get(0).getFieldName());
        for (int i = 1;i<fields.size();i++) {
            indexesBean.setIndexesColumn(indexesBean.getIndexesColumn()+","+fields.get(i).getFieldName());
        }
    }


    @Override
    public void clear(){
        indexesBean = null;
        indexesName.clear();
        indexesColumn.getSelectionModel().select(-1);
        indexesType.getSelectionModel().select(-1);
        tablespace.clear();
        columnTable.getItems().clear();
    }

    @Override
    public boolean isValid() {
        boolean matches = indexesName.getText().matches("^\\w+$");
        if (!matches){
            new Alert(Alert.AlertType.NONE, "索引命名不正确", new ButtonType[]{ButtonType.CLOSE}).show();
            return false;
        }else if (indexesType.getSelectionModel().getSelectedItem()==null){
            new Alert(Alert.AlertType.NONE, "请选择索引类型", new ButtonType[]{ButtonType.CLOSE}).show();
            return false;
        }else if (columnTable.getItems()==null||columnTable.getItems().size()<=0){
            new Alert(Alert.AlertType.NONE, "请选择索引字段", new ButtonType[]{ButtonType.CLOSE}).show();
            return false;
        }else
            return true;
    }

    @FXML
    private void eventAddColumnButtonClick(ActionEvent event) {
        if (indexesColumn.getSelectionModel().getSelectedIndex()!=-1&&
                !columnTable.getItems().contains(new IndexesWindowController.Field(indexesColumn.getSelectionModel().getSelectedItem()))) {
            columnTable.getItems().addAll(new IndexesWindowController.Field(indexesColumn.getSelectionModel().getSelectedItem()));
        }
    }

    @FXML
    private void eventDeleteColumnButtonClick(ActionEvent event) {
        if (columnTable.getSelectionModel().getSelectedIndex()!=-1){
            columnTable.getItems().remove(columnTable.getSelectionModel().getSelectedItem());
        }
    }
    public class Field{
        private final SimpleStringProperty fieldName = new SimpleStringProperty();


        public Field(String fieldName){
            this.fieldName.set(fieldName);
        }

        public String getFieldName() {
            return fieldName.get();
        }

        public SimpleStringProperty fieldNameProperty() {
            return fieldName;
        }

        public void setFieldName(String fieldName) {
            this.fieldName.set(fieldName);
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (obj instanceof PrimaryKeyWindowController.Field){
                PrimaryKeyWindowController.Field field = (PrimaryKeyWindowController.Field) obj;
                return StringUtils.equals(field.getFieldName(),this.getFieldName());
            }
            return false;

        }
    }
}
